package com.tcm.prescribe.service;

import com.base.core.network.SimpleHttpUtils;
import com.base.core.network.http.HttpHeader;
import com.base.core.network.http.ResponseEntity;
import com.tcm.prescribe.api.domain.dto.PrescribeDetailDTO;
import com.tcm.prescribe.api.domain.dto.PrescribeRecordDTO;
import com.tcm.prescribe.api.domain.enums.SexEnum;
import com.tcm.prescribe.api.service.WebService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WebServiceImpl implements WebService {


    //接口地址
    public static final String URL = "http://43.240.126.76:10080/HollysysApp/WebService.asmx/DataReceive";

    @Override
    public String createParams(PrescribeRecordDTO prescriberecord) {

        String xml = createXml(prescriberecord);

        Map<String, String> params = new HashMap<String, String>();
        params.put("xml", xml);
        HttpHeader header = new HttpHeader();
        header.add("Content-Type", "text/xml; charset=utf-8");
        ResponseEntity res = SimpleHttpUtils.post(URL, params, header, false);
        return  res.getBody();
    }

    private String createXml(PrescribeRecordDTO prescriberecord) {

        String xml = "<Prescriberecord>" +
                "<Id>" + prescriberecord.getId() + "</Id>" +
                "<RegisterId>" + prescriberecord.getRegisterId() + "</RegisterId>" +
                "<Name>" + prescriberecord.getName() + "</Name>" +
                "<Sex>" + SexEnum.getDesc(prescriberecord.getSex()) + "</Sex>" +
                "<Age>" + prescriberecord.getAge() + "</Age>" +
                "<Tele>" + prescriberecord.getTele() + "</Tele>" +
                "<Email></Email>" +
                "<DepartmentName>" + prescriberecord.getDepartmentName() + "</DepartmentName>" +
                "<DoctorName>" + prescriberecord.getDoctorName() + "</DoctorName>" +
                "<PrescriptionName>" + prescriberecord.getPrescriptionName() + "</PrescriptionName>" +
                "<PrescribeTime>" + prescriberecord.getPrescribeTime() + "</PrescribeTime>" +
                "<CreatorName>null</CreatorName>" +
                "<CreationTime>null</CreationTime>" +
                "<ValueSn></ValueSn>" +
                "<ValuerName></ValuerName>" +
                "<ValuationTime></ValuationTime>" +
                "<Price></Price>" +
                "<Quantity>" + prescriberecord.getQuantity() + "</Quantity>" +
                "<QuantityDay>" + prescriberecord.getQuantityDay() + "</QuantityDay>" +
                "<PriceTotal>" + prescriberecord.getPriceTotal() + "</PriceTotal>" +
                "<PaymentType>" + prescriberecord.getPaymentType() + "</PaymentType>" +
                "<PaymentStatus>PAYED</PaymentStatus>" +
                "<DataSource>HIS</DataSource>" +
                "<DeviceId>null</DeviceId>" +
                "<ProcessStatus>NEW</ProcessStatus>" +
                "<Description></Description>" +
                "<Prescribedetails>";
        List<PrescribeDetailDTO> prescribedetails = prescriberecord.getPrescribeDetails();
        for (PrescribeDetailDTO prescribedetail : prescribedetails) {

            xml += "<Prescribedetail>" +
                    "<Id>" + prescribedetail.getId() + "</Id>" +
                    "<No>" + prescribedetail.getNo() + "</No>" +
                    "<GranuleId>" + prescribedetail.getGranuleId() + "</GranuleId>" +
                    "<GranuleName>" + prescribedetail.getGranuleName() + "</GranuleName>" +
                    "<DoseHerb>" + prescribedetail.getDoseHerb() + "</DoseHerb>" +
                    "<Equivalent>" + prescribedetail.getEquivalent() + "</Equivalent>" +
                    "<Dose>" + prescribedetail.getDose() + "</Dose>" +
                    "<Price>" + prescribedetail.getPrice() + "</Price>" +
                    "</Prescribedetail>";
        }

        xml += "</Prescribedetails>" +
                "</Prescriberecord>";
        return xml;

    }

}
